#include "stdafx.h"

#include "threaded_progress_example.h"
#include "worker_thread.h"
#include <QVBoxLayout>
#include <QHBoxLayout>
#include <QProgressBar>
#include <QPushButton>
#include <QLabel>
#include <QMessageBox>
#include <QFont>

ThreadedProgressExample::ThreadedProgressExample(QWidget* parent)
    : QWidget(parent), workerThread(nullptr)
{
    setupUI();
    setupConnections();
}

ThreadedProgressExample::~ThreadedProgressExample()
{
    if (workerThread && workerThread->isRunning()) {
        workerThread->requestInterruption();
        workerThread->wait(1000);
    }
}

void ThreadedProgressExample::setupUI()
{
    setWindowTitle("Qt 多线程进度条示例");
    setFixedSize(400, 200);

    QVBoxLayout* mainLayout = new QVBoxLayout(this);

    // 标题
    QLabel* titleLabel = new QLabel("多线程进度演示");
    titleLabel->setAlignment(Qt::AlignCenter);
    QFont titleFont = titleLabel->font();
    titleFont.setPointSize(14);
    titleFont.setBold(true);
    titleLabel->setFont(titleFont);

    // 进度条
    progressBar = new QProgressBar();
    progressBar->setRange(0, 100);
    progressBar->setValue(0);
    progressBar->setFormat("进度: %p%");

    // 状态标签
    statusLabel = new QLabel("准备启动多线程任务");
    statusLabel->setAlignment(Qt::AlignCenter);

    // 按钮
    startButton = new QPushButton("开始多线程任务");
    stopButton = new QPushButton("停止任务");
    stopButton->setEnabled(false);

    QHBoxLayout* buttonLayout = new QHBoxLayout();
    buttonLayout->addWidget(startButton);
    buttonLayout->addWidget(stopButton);

    // 添加到布局
    mainLayout->addWidget(titleLabel);
    mainLayout->addSpacing(20);
    mainLayout->addWidget(progressBar);
    mainLayout->addWidget(statusLabel);
    mainLayout->addSpacing(20);
    mainLayout->addLayout(buttonLayout);
}

void ThreadedProgressExample::setupConnections()
{
    connect(startButton, &QPushButton::clicked, this, &ThreadedProgressExample::startThreadedTask);
    connect(stopButton, &QPushButton::clicked, this, &ThreadedProgressExample::stopThreadedTask);
}

void ThreadedProgressExample::startThreadedTask()
{
    progressBar->setValue(0);
    statusLabel->setText("多线程任务执行中...");
    startButton->setEnabled(false);
    stopButton->setEnabled(true);

    workerThread = new WorkerThread(this);

    connect(workerThread, &WorkerThread::progressUpdated, progressBar, &QProgressBar::setValue);
    connect(workerThread, &WorkerThread::progressUpdated, this, [this](int value) {
        statusLabel->setText(QString("处理中: %1%").arg(value));
        });
    connect(workerThread, &WorkerThread::taskCompleted, this, &ThreadedProgressExample::onTaskCompleted);
    connect(workerThread, &WorkerThread::taskError, this, &ThreadedProgressExample::onTaskError);
    connect(workerThread, &QThread::finished, workerThread, &QObject::deleteLater);

    workerThread->start();
}

void ThreadedProgressExample::stopThreadedTask()
{
    if (workerThread && workerThread->isRunning()) {
        workerThread->requestInterruption();
        stopButton->setEnabled(false);
        statusLabel->setText("正在停止任务...");
    }
}

void ThreadedProgressExample::onTaskCompleted(const QString& message)
{
    statusLabel->setText(message);
    startButton->setEnabled(true);
    stopButton->setEnabled(false);
    QMessageBox::information(this, "完成", message);
}

void ThreadedProgressExample::onTaskError(const QString& error)
{
    statusLabel->setText(error);
    startButton->setEnabled(true);
    stopButton->setEnabled(false);
    QMessageBox::critical(this, "错误", error);
}